fn main() {
    println!("=== Rust迭代器API示例 ===");

    // 1. 创建迭代器
    println!("\n1. 创建迭代器");
    let v = vec![1, 2, 3, 4, 5];
    let iter = v.iter();  // 从Vec创建迭代器
    println!("原始数组: {:?}", v);

    // 2. map转换
    println!("\n2. map转换");
    let doubled: Vec<i32> = v.iter()
        .map(|x| x * 2)
        .collect();
    println!("所有元素乘2: {:?}", doubled);

    // 3. filter过滤
    println!("\n3. filter过滤");
    let even: Vec<&i32> = v.iter()
        .filter(|x| *x % 2 == 0)
        .collect();
    println!("偶数: {:?}", even);

    // 4. chain连接
    println!("\n4. chain连接");
    let v2 = vec![6, 7, 8];
    let chained: Vec<&i32> = v.iter().chain(v2.iter()).collect();
    println!("连接后的数组: {:?}", chained);

    // 5. enumerate添加索引
    println!("\n5. enumerate添加索引");
    println!("带索引的元素:");
    for (i, val) in v.iter().enumerate() {
        println!("索引 {}: {}", i, val);
    }

    // 6. zip组合
    println!("\n6. zip组合");
    let names = vec!["苹果", "香蕉", "橙子"];
    let prices = vec![5.0, 3.0, 4.0];
    println!("水果价格表:");
    for (name, price) in names.iter().zip(prices.iter()) {
        println!("{}: ￥{}", name, price);
    }

    // 7. fold累积
    println!("\n7. fold累积");
    let sum = v.iter().fold(0, |acc, x| acc + x);
    println!("数组求和: {}", sum);

    // 8. take和skip
    println!("\n8. take和skip操作");
    let first_three: Vec<&i32> = v.iter().take(3).collect();
    println!("前三个元素: {:?}", first_three);
    let skip_two: Vec<&i32> = v.iter().skip(2).collect();
    println!("跳过前两个元素: {:?}", skip_two);

    // 9. all和any
    println!("\n9. all和any判断");
    let all_positive = v.iter().all(|&x| x > 0);
    let any_even = v.iter().any(|x| x % 2 == 0);
    println!("是否全部为正数: {}", all_positive);
    println!("是否存在偶数: {}", any_even);

    // 10. rev反转
    println!("\n10. rev反转");
    let reversed: Vec<&i32> = v.iter().rev().collect();
    println!("反转后: {:?}", reversed);
}